//dom加载后执行
$(document).ready(function(){

});

//定义验证对象
var clicktype = {
    'user_name':'',
    'user_nick':'',
    'user_phone':'',
    'user_password':'',
    'repassword':'',
    'veryfy':''
};
var uname = $('#user_name');
var unick = $('#user_nick');
var uphone = $('#user_phone');
var upass = $('#user_password');
var repass = $('#repassword');
var veryfy = $('#veryfy');
//不取消点击按钮,但在点击时进行验证
//获取焦点时进入编辑状态,取消正确或错误状态

//校验用户名
uname.blur(function(){
    $.ajax({
        url:'/ajax',
        type:'POST',
        data:{"type":"user_name","input":uname.val()},
        async:true,
        success:function(ev){
          if (ev==1) {
            uname.css("background-color","rgb(169, 240, 180)");
            clicktype.user_name = true;
          } else {
            uname.css("background-color","rgb(255, 185, 185)");
            uname.val('');
            clicktype.user_name = false;
          }
        },
        error:function(ev){

        }
    });
});

//用户名框被选中时
uname.focus(function(){
  uname.css("background-color","#CCC");
});

//校验用户昵称
unick.blur(function(){
    $.ajax({
        url:'/ajax',
        type:'POST',
        data:{"type":"user_nick","input":unick.val()},
        async:true,
        success:function(ev){
          if (ev==1) {
            unick.css("background-color","rgb(169, 240, 180)");
            clicktype.user_nick = true;
          } else {
            unick.css("background-color","rgb(255, 185, 185)");
            unick.val('');
            clicktype.user_nick = false;
          }
        },
        error:function(ev){

        }
    });
});

//用户昵称获取焦点时
unick.focus(function(){
    unick.css("background-color","#CCC");
});

//验证手机号
uphone.blur(function(){
    var match = /^1[34578]{1}\d{9}$/;
    var ev = match.test(uphone.val())?1:0;
    if (ev==1) {
        //还得发ajax,判断手机号是否存在
        uphone.css("background-color","rgb(169, 240, 180)");
        clicktype.user_phone = true;
    } else {
        uphone.css("background-color","rgb(255, 185, 185)");
        uphone.val('');
        clicktype.user_phone = false;
    }
});

//用户手机获取焦点时
uphone.focus(function(){
    uphone.css("background-color","#CCC");
});

//验证用户密码
upass.blur(function(){
    var match = /[A-Za-z0-9]{6,12}/;
    var ev = match.test(upass.val())?1:0;
    if (ev==1) {
        upass.css("background-color","rgb(169, 240, 180)");
        clicktype.user_password = true;
    } else {
        repass.css("background-color","rgb(255, 185, 185)");
        upass.css("background-color","rgb(255, 185, 185)");
        upass.val('');
        repass.val('');
        clicktype.user_password = false;
    }
});

//用户密码获取焦点时
upass.focus(function(){
    upass.css("background-color","#efefef");
    repass.css("background-color","#CCC");
});

//用户验证密码获取焦点时
repass.blur(function(){
    if (repass.val() === upass.val() && repass.val() !== '') {
        repass.css("background-color","rgb(169, 240, 180)");
        upass.css("background-color","rgb(169, 240, 180)");
        clicktype.repassword = true;
    } else {
        repass.css("background-color","rgb(255, 185, 185)");
        upass.css("background-color","rgb(255, 185, 185)");
        repass.val('');
        upass.val('');
        clicktype.repassword = false;
    }
});

//用户验证密码获取焦点时
repass.focus(function(){
    repass.css("background-color","#efefef");
    upass.css("background-color","#efefef");
});

//验证验证码
veryfy.blur(function(){
    $.ajax({
        url:'/checkimg',
        type:'POST',
        data:veryfy,
        async:true,
        success:function(ev){
            if (ev==1) {
                veryfy.css("background-color","rgb(169, 240, 180)");
                clicktype.veryfy = true;
            } else {
                veryfy.css("background-color","rgb(255, 185, 185)");
                veryfy.val('');
                clicktype.veryfy = false;
            }
        },
        error:function(ev){}
    });
});

//用户验证图片验证码获取焦点时
veryfy.focus(function(){
    veryfy.css("background-color","#CCC");
});

//刷新验证码
$('#verifyimg').click(function(){
  $('#verifyimg').attr('src',"/verify?id="+Math.random());
});

//绑定提交按钮的错误提示
$('#sub').click(function(){
    $res = clicktype.veryfy && clicktype.repassword && clicktype.user_password && clicktype.user_phone && clicktype.user_nick && clicktype.user_name;
    if(!$res){
        console.log(clicktype);
        alert('重新提交');
        return false;
    }
});

//绑定返回登陆路由
$('#logbtn').click(function(){
  window.location.href="http://meiqiu.yuanbuyu.com/user/login";
<script src="home/js/jquery.accordion.js"></script>
});
